package common

import (
	"context"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/common/dmp_report"
	"git.myscrm.cn/golang/stark/v4"
)

type DmpReportServer struct {
	dmpReportService dmp_report.DmpReportServiceIface
}

func NewDmpReportServer(dmpReportService dmp_report.DmpReportServiceIface) pb.DmpReportServiceServer {
	return &DmpReportServer{
		dmpReportService: dmpReportService,
	}
}

func (r *DmpReportServer) GetDmpLoginUrl(ctx context.Context, request *pb.DmpLoginUrlRequest) (*pb.DmpLoginUrlResponse, error) {
	path, err := r.dmpReportService.GetDmpPathByCode(request.GetPageCode())
	if err != nil {
		stark.Logger.Error(ctx, "获取页面 code 失败", err.Error())
		return nil, err
	}
	params := make(map[string]string, 3)
	params["province_code"] = request.GetProvinceCode()
	params["date"] = request.GetYear()
	params["tenant_code"] = request.GetTenantCode()
	// 可选参数
	if request.GetBusinessId() != "" {
		params["business_id"] = request.GetBusinessId()
	}
	if request.GetStartDate() != "" && request.GetEndDate() != "" {
		params["start_time"] = request.GetStartDate()
		params["end_time"] = request.GetEndDate()
	}
	url, err := r.dmpReportService.GetSsoLoginUrl(ctx, path, params)
	if err != nil {
		stark.Logger.Error(ctx, "获取 dmp 单点登录 url 失败", err.Error())
		return nil, err
	}

	return &pb.DmpLoginUrlResponse{
		Url: url,
	}, nil
}
